/*
Copyright (c) 2010, Andri Yngvason
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of ArDAS nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <avr/io.h>
#include <util/delay.h>

#include "../gpio.h"
#include "../cmd.h"
#include "shtXX.h"


static void *shtXX_cmd_list[] = {
	"st", &shtXX_cmd_samplet,
	"srh", &shtXX_cmd_samplerh,
	"read", &shtXX_cmd_read,
	NULL
};


#define SHTXX_RH 0x05
#define SHTXX_TEMP 0x03


int16_t shtXX_rx(int, int);


int16_t shtXX_rx(int sda, int scl) {
	uint8_t data = 0;
	int i;

	i = 8;
	while(i-- > 0) {
		gpio_ddr(scl, 0);	// scl up
		_delay_us(10);
		data |= gpio_get(sda) << i;	// get databit
		_delay_us(10);
		gpio_ddr(scl, 1);	// scl down
		_delay_us(10);
	}

	/* send ack */
	gpio_ddr(sda, 1);	// sda down
	_delay_us(10);
	gpio_ddr(scl, 0);	// scl up
	_delay_us(10);
	gpio_ddr(scl, 1);	// scl down
	_delay_us(10);
	gpio_ddr(sda, 0);	// sda up
	_delay_us(10);

	return data;
}


int shtXX_sample(int t, int sda, int scl) {
	int ack, i;
	uint8_t data;

	if(t == 0)
		data = SHTXX_RH;
	else
		data = SHTXX_TEMP;

	gpio_set(sda, 0);
	gpio_set(scl, 0);	// sda, scl init
	gpio_ddr(sda, 0);
	gpio_ddr(scl, 0);	// sda, scl up
	_delay_us(10);
	
	/* start-stop */
	gpio_ddr(sda, 1);	// sda down
	_delay_us(10);
	gpio_ddr(scl, 1);	// scl down
	_delay_us(10);
	gpio_ddr(scl, 0);	// scl up
	_delay_us(10);
	gpio_ddr(sda, 0);	// sda up
	_delay_us(10);
	gpio_ddr(scl, 1);	// scl down
	_delay_us(10);
	gpio_ddr(sda, 1);	// sda down
	_delay_us(10);
	
	/* send request */
	i = 8;
	while(i-- > 0) {
		if(data & (1 << i))
			gpio_ddr(sda, 0);	// sda up
		else
			gpio_ddr(sda, 1);	// sda down

		_delay_us(10);
		gpio_ddr(scl, 0);	// scl up
		_delay_us(10);
		gpio_ddr(scl, 1);	// scl down
		_delay_us(10);
	}

	gpio_ddr(sda, 0);
	gpio_ddr(scl, 0);	// sda, scl up
	_delay_us(10);
	ack = !gpio_get(sda);	// check ack
	_delay_us(10);
	gpio_ddr(scl, 1);	// scl down
	_delay_us(10);

	if(!ack)
		return 0;

	return 1;
}


int16_t shtXX_read(int sda, int scl) {
	int tm;
	uint8_t din0, din1, crc;
	int16_t result;
	//_delay_ms(10);
	tm = 0;
	/* Wait for signal */
	while(gpio_get(sda)) {
		_delay_ms(1);
		/* Check timout */
		if(tm++ > 400)
			return -1;
	}

	din1 = shtXX_rx(sda, scl);
	din0 = shtXX_rx(sda, scl);
	/* TODO: integrate crc */
	crc = shtXX_rx(sda, scl);
	
	result = din1;
	result <<= 8;
	result |= din0;
	return result;
}


int shtXX_cmd(char *line) {
	int (*f)(char*);
	f = cmd_find(&line,shtXX_cmd_list,&strcmp);

	if(f == cmd_notfound) {
		printf("ERROR: Sensor command not found.\n");
		return 1;
	}

	if(f == NULL) {
		return 0;
	}

	return f(line);
}


int shtXX_cmd_samplerh(char *line) {
	char *sda;
	char *scl;

	sda = strsep(&line, " \n\r");
	scl = strsep(&line, " \n\r");
	if(sda == NULL || scl == NULL) {
		printf("ERROR: sensor shtXX sample rh: please specify clock and data pins.\n");
		return 1;
	}

	if(!shtXX_sample(0, atoi(sda), atoi(scl))) {
		printf("ERROR: no 2-wire device present on sda %s with scl %s.\n", sda, scl);
		return 1;
	}

	return 0;
}


int shtXX_cmd_samplet(char *line) {
	char *sda;
	char *scl;

	sda = strsep(&line, " \n\r");
	scl = strsep(&line, " \n\r");
	if(sda == NULL || scl == NULL) {
		printf("ERROR: sensor shtXX sample T: please specify clock and data pins.\n");
		return 1;
	}

	if(!shtXX_sample(1, atoi(sda), atoi(scl))) {
		printf("ERROR: no 2-wire device present on sda %s with scl %s.\n", sda, scl);
		return 1;
	}

	return 0;
}


int shtXX_cmd_read(char *line) {
	char *sda;
	char *scl;
	int16_t data;

	sda = strsep(&line, " \n\r");
	scl = strsep(&line, " \n\r");
	if(sda == NULL || scl == NULL) {
		printf("ERROR: sensor shtXX read: please specify clock and data pins.\n");
		return 1;
	}

	data = shtXX_read(atoi(sda), atoi(scl));
	if(data < 0) {
		printf("ERROR: sensor shtXX read: timeout reached.\n");
		return 1;
	}

	printf("shtXX[sda:%s, scl:%s]: %d\n", sda, scl, data);

	return 0;
}

